package de.preiscrawler.parser;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;
import de.preiscrawler.AlleAnbieter;
import de.preisrobot.entity.Artikel;

public class RedCoonParser implements Parser {

	@Override
	public List<Artikel> parse(String sourceUrlString) throws Exception {
		Source source = new Source(new URL(sourceUrlString));

		return parse(source);
	}

	/**
	 * <li id="container_B118311" class="item">
	 * <div class="product"> <div class="unit picturebox"> <div
	 * id="extras_B118311" class="unit extras"> <div id="data_B118311"
	 * class="unit data"> </div>
	 * 
	 * @param source
	 * @return
	 */
	private List<Artikel> parse(Source source) {
		List<Artikel> list = new ArrayList<Artikel>();
		List<Element> alleElements = source.getAllElementsByClass("item");

		for (Element element : alleElements) {
			Artikel a = new Artikel();

			
			List<Element> img = element.getFirstElement().getAllElements("img");
			Element elemImg = img.get(0);
			
			String imgUrl = elemImg.getAttributeValue("src");
			String hersteller = elemImg.getAttributeValue("alt");
			String artikel_Nr =element.getAllElementsByClass("productId sllg").get(0).getTextExtractor().toString();
			String desc =getDesc(element);
			
			List<Element> data = element.getAllElementsByClass("unit data");
			Element dataBody = data.get(0);
			Element firstElement = dataBody.getFirstElement("a");
			String artikellink = firstElement.getAttributeValue("href");
			String kurzBez = firstElement.getTextExtractor().toString();
			String preis = parsePreis(element);
			
			
			a.setAnbieterId(getAnbieter());
			a.setImageLink(imgUrl);
			a.setArtikelNr(artikel_Nr.split(":")[1]);
			a.setDesc(desc);
			a.setHersteller(hersteller);
			a.setArtikelLink(artikellink);
			a.setDesc(kurzBez);
			a.setPreis(preis);
			
			list.add(a);
		
		}

		return list;
	}

	private String parsePreis(Element element) {
 
		List<Element> allElementsByClass = element.getAllElementsByClass("lb-price");
		Element element2 = allElementsByClass.get(0);
		List<Element> allElements = element2.getAllElements("img");
		
		String value = "";
		boolean hasDecimalDot = false;
		for (Element imgPreislInk : allElements) {
			
			imgPreislInk.toString();
			String preisStr = imgPreislInk.getAttributeValue("alt");
			String attributeValue2 = imgPreislInk.getAttributeValue("src");
			if(attributeValue2==null||preisStr == null 
					|| attributeValue2.contains("euro")
					|| preisStr.contains(",")
					|| preisStr.contains("-")) // some img we do not need
				continue;
			
			if(attributeValue2.contains("small")){
				if(!hasDecimalDot){
					value = value +".";
					hasDecimalDot=true;
				}
				
			}
			
			value = value +preisStr;
			
		}
		return value;
	}

	private String getDesc(Element element) {
		List<Element> techPoints = element.getAllElementsByClass("pd-bullets");
		String desc="";
		for (Element element2 : techPoints) {
			String desc2 = element2.toString();
			desc= desc +desc2+"\n";
		}
		
		return desc;
	}

	@Override
	public int getAnbieter() {

		return AlleAnbieter.REDCOON;
	}

}
